package com.tb.shoop.controller.config.Filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 加密过滤器
 */
@Slf4j
public class EncryptFilter implements Filter {

    private String accessKeyId = "sysnb3agVjS9jdAl";
    private String accessSecret = "aO1KVQ20FY9bJ9dfs75ODsIYJPOD2Cc6Hx5lFO66VGFkmGcy";

    //定义不需要走加密接口
/*    private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
            Arrays.asList("/order/getOrderList", "/order/getSignOrderList", "/product/getProductList",
                    "/product/getSingleProduct", "/order/getUserMoney", "/product/getOrderHot",
                    "/app/task/statisticsRechargeTask", "/app/task/statisticsOrderTask", "/app/task/statisticsCommentTask",
                    "/trans/getTransferCallBack","/recharge/getRechargeMoneyCallBack")));*/


    /**
     * Default constructor.
     */
    public EncryptFilter() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        //response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request,lang");
        //访问控制允许凭据，true为允许
        response.setHeader("Access-Control-Allow-Credentials", "true");
        String method = ((HttpServletRequest) servletRequest).getServletPath();
        filterChain.doFilter(request, response);
        return;
    }


    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}
